## django自带的库
from django.db import models
# Create your models here.
from django.utils.html import format_html

### python自带的库
from datetime import datetime


## 第三方库



class Device(models.Model):
    ip = models.CharField(max_length=255,verbose_name="设备IP")
    dev_name = models.CharField(max_length=255,verbose_name="设备名称",)
    diqu = models.CharField(max_length=255,verbose_name="地区")
    weizhi = models.CharField(max_length=255,verbose_name="位置")
    jigui = models.CharField(max_length=255,default='1号',verbose_name="机柜")
    xinghao = models.CharField(max_length=255,verbose_name="设备型号",)
    sn = models.CharField(max_length=255,blank=True,verbose_name="序列号")
    username = models.CharField(max_length=255,verbose_name="远程用户")
    password = models.CharField(max_length=255,verbose_name="远程密码")
    ssh_port = models.IntegerField(default=22,verbose_name="远程端口")
    created_date = models.DateTimeField(verbose_name="创建日期",default=datetime.now)
    modified_date = models.DateTimeField(verbose_name="修改日期", default=datetime.now)
    #定义选择栏，添加设备时可选择设备类型
    VENDOR_CHOICES=(
        ('huawei','华为'),
        ('cisco','思科'),
        ('H3c', '华三'),
        ('other', '其他'),
    )
    vendor = models.CharField(max_length=255, choices=VENDOR_CHOICES,verbose_name="厂商")
    VENDOR1_CHOICES = (
        ('server', '服务器'),
        ('firewalld', '防火墙'),
        ('switch', '交换机'),
        ('router', '路由器'),
        ('other', '其他'),
    )
    leixing = models.CharField(max_length=255, choices=VENDOR1_CHOICES, verbose_name="设备类型")

    ENTER_CHOICES = (
        ('1', '手工录入'),
        ('2', '自动发现'),
    )
    enter = models.CharField(max_length=64,verbose_name="录入方式",choices=ENTER_CHOICES,default=1)
    snmp = models.CharField(max_length=255,verbose_name="SNMP密码")

    class Meta:
        verbose_name = '设备管理'
        verbose_name_plural = '设备管理'



    def __str__(self):
        return f"{self.ip}"


class DevBackups(models.Model):
    title = models.CharField(max_length=255,verbose_name="备份名称")
    time = models.DateTimeField(max_length=255,null=True,verbose_name="备份日期")
    ip = models.CharField(null=True,verbose_name="设备IP",max_length=255)

    class Meta:
        # verbose_name = '设备管理'
        verbose_name_plural = '备份下载'

    # def save(self, *args, **kwargs):
    #     # 在保存之前对标题进行处理
    #     if self.title:
    #         self.title = self.title.strip().upper()  # 去除前后空格并转换为大写
    #     super().save(*args, **kwargs)


class monitor(models.Model):

    net_ip = models.CharField(max_length=64,verbose_name="设备IP")
    cpu_info = models.IntegerField(verbose_name="CPU",)
    mem_info = models.CharField(verbose_name="mem", max_length=64)
    temp = models.CharField(null=True,max_length=64,verbose_name="设备温度")
    time = models.DateTimeField(null=True,verbose_name="时间")

    class Meta:
        verbose_name = '设备状态'
        verbose_name_plural = '设备状态'


    ##  可以通过函数方式定义字段值
    def Cpu_Speed(self):
        ## 通过html语法返回值加工处理
        return format_html('<progress value={} title="已使用{}%" max="100" ></progress>', self.cpu_info,self.cpu_info)

    def Mem_Speed(self):
        ## 通过html语法返回值加工处理
        return format_html('<progress value={} max="100"></progress>', self.mem_info)

    Cpu_Speed.short_description = "CPU利用率"
    Mem_Speed.short_description = "Mem利用率"


class inter(models.Model):
    inters =  models.CharField(max_length=64,verbose_name="接口")
    state = models.CharField(max_length=64,verbose_name="接口状态")
    protocol = models.CharField(max_length=64,verbose_name="协议状态")
    inuti = models.CharField(max_length=64,verbose_name="入接口利用率")
    oututi = models.CharField(max_length=64,verbose_name="出接口利用率")
    inerros = models.CharField(max_length=64,verbose_name="入接口错误率")
    outerros = models.CharField(max_length=64,verbose_name="出接口错误率")

    class Meta:
        verbose_name = '接口信息'
        verbose_name_plural = '接口信息'


class inter_count(models.Model):
    ip =  models.CharField(max_length=255,verbose_name="设备IP")
    inter_count = models.CharField(max_length=64,verbose_name="接口总数")
    up = models.CharField(max_length=64,verbose_name="已使用数量")
    down = models.CharField(max_length=64,verbose_name="未使用数量")

    class Meta:
        verbose_name = '接口总计'
        verbose_name_plural = '接口总计'